/**
 @file pCam_osd.c
 @brief OSD general control Function
 @author 
 @version 1.0
*/
//==============================================================================
//
//                              INCLUDE FILE
//
//============================================================================== 

#include "includes_fw.h"
#include "pCam_osd_api.h"
#include "pCam_msg.h"
#include "lib_retina.h"

#if (SUPPORT_OSD_FUNC)

//==============================================================================
//
//                              GLOBAL VARIABLE
//
//==============================================================================

MMP_BYTE  strTimeDate[64];
MMP_USHORT nPcamOsdTimerCount;
MMP_UBYTE gbOSDTimeFormatChange = 0, gbOSDWordCntChange = 0;

static PCAM_RTC_TIME    m_sRTCTime;
#if (MAINTAIN_RTC_TIME_METHOD==USE_SW_TIMER_TRIGGER)
static MMP_ULONG    nRTCUpdateTimeID;
#endif
static MMP_USHORT   m_usCurrentW = 1280, m_usCurrentH = 720;
MMP_USHORT          m_usOsdStartXOffset = 0, m_usOsdStartYoffset = 0;
static MMP_BOOL     gbSetDateTimeComplete = 0;
extern MMP_BOOL     gbOSDDisplayEn;
extern MMP_BYTE strCustomerData[32];
#if (CUSTOMER == ANW)
MMP_UBYTE ubOSDDateFmt = PCAM_ACC_TIMESTAMP_FORMAT_1;
#else
MMP_UBYTE ubOSDDateFmt = PCAM_ACC_TIMESTAMP_FORMAT_2;
#endif
MMP_UBYTE ubOSDTimeFmt = 0;  // 0: 24 Hour, 1: 12 Hour
MMP_BOOL AM0_PM1_state = 0;  // 0: AM, 1: PM
static MMP_BOOL bTimeFmtflag = 1;

//==============================================================================
//
//                               FUNCTION PROTOTYPE
//
//==============================================================================

//extern void dbg_printf(unsigned long level, char *fmt, ...);

//==============================================================================
//
//                               FUNCTIONS
//
//==============================================================================

void pcam_Int2Str(MMP_ULONG value, MMP_BYTE *string)
{
    MMP_ULONG i,j;
    MMP_BYTE   temp[16];
    
    for(i=0;i<16;i++) {
        if ( (value / 10) != 0 ) {
            temp[i] = '0' + value % 10;
            
            value = value / 10;
        }
        else {
            temp[i] = '0' + value % 10;
            break;
        }
    }
    
    for(j=0;j<(i+1);j++) {
        string[j] = temp[i - j];
    }
    
    string[j] = 0;
}


#if (MAINTAIN_RTC_TIME_METHOD==USE_SW_TIMER_TRIGGER)
void pcam_UpdateTime_StartTimer(void)
{
    nRTCUpdateTimeID = MMPF_OS_StartTimer(1000, OS_TMR_OPT_PERIODIC, (OS_TMR_CALLBACK)&pcam_RTC_UpdateTime, NULL);

    if((nRTCUpdateTimeID == 0xFF) || (nRTCUpdateTimeID == 0xFE)){
        MMPF_OS_StopTimer(nRTCUpdateTimeID, OS_TMR_OPT_NONE);
        dbg_printf(1, "PCAM_StartTimer_RTCUpdateTime(), create timer error! \r\n");
    }
}

void pcam_UpdateTime_StotTimer(void)
{
    if((nRTCUpdateTimeID != 0xFF) && (nRTCUpdateTimeID != 0xFE)){
        MMPF_OS_StopTimer(nRTCUpdateTimeID, OS_TMR_OPT_NONE);
    }
}
#endif

void _____OSD_Function_________(void){return;} //dummy

MMP_USHORT pcam_Osd_ConfigCurrentStream(MMP_USHORT pwCurrentW, MMP_USHORT pwCurrentH)
{
	m_usCurrentW 	= pwCurrentW;
	m_usCurrentH 	= pwCurrentH;
	
	return PCAM_ERROR_NONE;
}

MMP_UBYTE gbOSDForeColorY = OSD_COLOR_BLACK_Y;
MMP_USHORT gsOSDForeColorUV = OSD_COLOR_BLACK_UV;
MMP_UBYTE gbOSDBackColorY = OSD_COLOR_WHITE_Y;
MMP_USHORT gsOSDBackColorUV = OSD_COLOR_WHITE_UV;

extern MMP_USHORT gsOSD_WIDTH_MAX_WORD;

MMP_USHORT pcam_Osd_TranslateRGB2YUV(MMP_UBYTE inR, MMP_UBYTE inG, MMP_UBYTE inB, MMP_UBYTE *outY, MMP_USHORT *outUV)
{
    MMP_SHORT Y,U,V;

    Y = (MMP_SHORT)(( 66*inR + 129*inG + 25*inB)/256) + 16; // 20 bits
    if(Y > 255) Y = 255;
    else if(Y < 0) Y = 0;
    
    U = (MMP_SHORT)(( -38*inR -  74*inG + 112*inB)/256) + 128; // 20 bits
    if(U > 255) U = 255;
    else if(U < 0) U = 0;

    V = (MMP_SHORT)(( 112*inR - 94*inG -  18*inB)/256) + 128; // 20 bits
    if(V > 255) V = 255;
    else if(V < 0) V = 0;
            
    *outY = (MMP_UBYTE)Y;
    *outUV = (MMP_USHORT)(U + ((MMP_USHORT)V << 8));

    return PCAM_ERROR_NONE;
}

/**
@brief	transfer psRtcTime(int) to pdDateTime(string)

*/
MMP_USHORT pcam_TransferDateTime2String(PCAM_RTC_TIME* psRtcTime, MMP_BYTE* pdDateTime, MMP_UBYTE byDateConfig, MMP_UBYTE byFormatConfig)
{
	PCAM_TIME_STRING sTimeString;
		
	pcam_Int2Str(psRtcTime->uwYear   , sTimeString.byYear);
    pcam_Int2Str(psRtcTime->uwMonth  , sTimeString.byMonth);
    pcam_Int2Str(psRtcTime->uwDay    , sTimeString.byDay);
    pcam_Int2Str(psRtcTime->uwHour   , sTimeString.byHour);
    pcam_Int2Str(psRtcTime->uwMinute , sTimeString.byMinute);
    pcam_Int2Str(psRtcTime->uwSecond , sTimeString.bySecond);
    
    if (PCAM_ACC_TIMESTAMP_TIME_ONLY != byDateConfig) {

        #if (CUSTOMER == ANW)
            STRCAT(pdDateTime, " ");  // add one space
        #endif
            
	    if  ( byFormatConfig == PCAM_ACC_TIMESTAMP_FORMAT_1 ){

            if(psRtcTime->uwYear < 1000)
                STRCAT(pdDateTime, "0");
            else if(psRtcTime->uwYear < 100)
                STRCAT(pdDateTime, "00");
            else if(psRtcTime->uwYear < 10)
                STRCAT(pdDateTime, "000");
	        STRCAT(pdDateTime, sTimeString.byYear);
	        STRCAT(pdDateTime, "-");
	        
	        if(psRtcTime->uwMonth < 10)
	            STRCAT(pdDateTime, "0");
	            
	        STRCAT(pdDateTime, sTimeString.byMonth);
	        STRCAT(pdDateTime, "-");
	        
	        if(psRtcTime->uwDay < 10)
	            STRCAT(pdDateTime, "0");
	            
	        STRCAT(pdDateTime, sTimeString.byDay);
	    }
	    else if  ( byFormatConfig == PCAM_ACC_TIMESTAMP_FORMAT_2 ){
	        
            if(psRtcTime->uwYear < 1000)
                STRCAT(pdDateTime, "0");
            else if(psRtcTime->uwYear < 100)
                STRCAT(pdDateTime, "00");
            else if(psRtcTime->uwYear < 10)
                STRCAT(pdDateTime, "000");
	        STRCAT(pdDateTime, sTimeString.byYear);
	        STRCAT(pdDateTime, "/");
	        
	        if(psRtcTime->uwMonth < 10)
	            STRCAT(pdDateTime, "0");
	            
	        STRCAT(pdDateTime, sTimeString.byMonth);
	        STRCAT(pdDateTime, "/");
	        
	        if(psRtcTime->uwDay < 10)
	            STRCAT(pdDateTime, "0");
	            
	        STRCAT(pdDateTime, sTimeString.byDay);
	    }
	    else if  ( byFormatConfig == PCAM_ACC_TIMESTAMP_FORMAT_3 ){
	    
	        if(psRtcTime->uwDay < 10)
	            STRCAT(pdDateTime, "0");
	            
	        STRCAT(pdDateTime, sTimeString.byDay);
	        STRCAT(pdDateTime, "/");
	        
	        if(psRtcTime->uwMonth < 10)
	            STRCAT(pdDateTime, "0");
	            
	        STRCAT(pdDateTime, sTimeString.byMonth);
	        STRCAT(pdDateTime, "/");
            if(psRtcTime->uwYear < 1000)
                STRCAT(pdDateTime, "0");
            else if(psRtcTime->uwYear < 100)
                STRCAT(pdDateTime, "00");
            else if(psRtcTime->uwYear < 10)
                STRCAT(pdDateTime, "000");
	        STRCAT(pdDateTime, sTimeString.byYear);
	    }
	    else if  ( byFormatConfig == PCAM_ACC_TIMESTAMP_FORMAT_4 ){
	    
	        if(psRtcTime->uwMonth < 10)
	            STRCAT(pdDateTime, "0");
	            
	        STRCAT(pdDateTime, sTimeString.byMonth);
	        STRCAT(pdDateTime, "/");
	        
	        if(psRtcTime->uwDay < 10)
	            STRCAT(pdDateTime, "0");
	            
	        STRCAT(pdDateTime, sTimeString.byDay);
	        STRCAT(pdDateTime, "/");
            if(psRtcTime->uwYear < 1000)
                STRCAT(pdDateTime, "0");
            else if(psRtcTime->uwYear < 100)
                STRCAT(pdDateTime, "00");
            else if(psRtcTime->uwYear < 10)
                STRCAT(pdDateTime, "000");
	        STRCAT(pdDateTime, sTimeString.byYear);
	    }
	    
	    if (PCAM_ACC_TIMESTAMP_DATE_AND_TIME == (byDateConfig & PCAM_ACC_TIMESTAMP_DATE_MASK)) {
            if(ubOSDTimeFmt) {  //12Hour
                if((psRtcTime->uwHour > 11) && bTimeFmtflag) {
                    AM0_PM1_state = (!AM0_PM1_state);
                    bTimeFmtflag = 0;
                }
                if(psRtcTime->uwHour > 12) {
                    psRtcTime->uwHour -= 12;
        	        pcam_Int2Str(psRtcTime->uwHour, sTimeString.byHour);
        	        bTimeFmtflag = 1;
                }
                if(psRtcTime->uwHour == 0) {
                    psRtcTime->uwHour += 12;
        	        pcam_Int2Str(psRtcTime->uwHour, sTimeString.byHour);
                    AM0_PM1_state = 0;
                }
	    
	            STRCAT(pdDateTime, " ");
	        
    	        if(psRtcTime->uwHour < 10)
	                STRCAT(pdDateTime, "0");
	            
    	        STRCAT(pdDateTime, sTimeString.byHour);
	            STRCAT(pdDateTime, ":");
	        
    	        if(psRtcTime->uwMinute < 10)
    	            STRCAT(pdDateTime,"0");
	            
    	        STRCAT(pdDateTime, sTimeString.byMinute);
    	        STRCAT(pdDateTime, ":");
	        
    	        if(psRtcTime->uwSecond < 10)
	                STRCAT(pdDateTime,"0");
	            
    	        STRCAT(pdDateTime, sTimeString.bySecond);

        #if (CUSTOMER == ANW)
                STRCAT(pdDateTime, " ");  // add one space
        #endif
        
                if(AM0_PM1_state)
                    STRCAT(pdDateTime, "PM ");  //PM
                else
                    STRCAT(pdDateTime, "AM ");  //AM
            }
            else {  //24Hour
                if(AM0_PM1_state) {
                    if(bTimeFmtflag) {
                        psRtcTime->uwHour += 12;
                    }
                    else {
                        bTimeFmtflag = 1;
                    }
                    pcam_Int2Str(psRtcTime->uwHour, sTimeString.byHour);
                    AM0_PM1_state = 0;
                }
                else if(bTimeFmtflag == 0) {
                    psRtcTime->uwHour -= 12;
                    pcam_Int2Str(psRtcTime->uwHour, sTimeString.byHour);
                    bTimeFmtflag = 1;
                }

                STRCAT(pdDateTime, " ");

                if(psRtcTime->uwHour < 10)
                    STRCAT(pdDateTime, "0");

                STRCAT(pdDateTime, sTimeString.byHour);
                STRCAT(pdDateTime, ":");

                if(psRtcTime->uwMinute < 10)
                    STRCAT(pdDateTime,"0");

                STRCAT(pdDateTime, sTimeString.byMinute);
                STRCAT(pdDateTime, ":");

                if(psRtcTime->uwSecond < 10)
                    STRCAT(pdDateTime,"0");

                STRCAT(pdDateTime, sTimeString.bySecond);
                
                STRCAT(pdDateTime, "   ");
            }
	    }
	}
	else {
	    if(psRtcTime->uwHour < 10)
	        STRCPY(pdDateTime, "0");

        STRCPY(pdDateTime, sTimeString.byHour);
	    STRCAT(pdDateTime, ":");
	    
	    if(psRtcTime->uwMinute < 10)
	        STRCAT(pdDateTime, "0");
	        
	    STRCAT(pdDateTime, sTimeString.byMinute);
	    STRCAT(pdDateTime, ":");
	    
	    if(psRtcTime->uwSecond < 10)
	        STRCAT(pdDateTime, "0");
	            
	    STRCAT(pdDateTime, sTimeString.bySecond);
	}
	
	return PCAM_ERROR_NONE;
}

extern unsigned char FontTable8x10[32][10];
#if (CUSTOMER == ANW)
extern unsigned char FontTable12x14[32][2*14];
extern unsigned char FontTable6x8[32][8];
#else
extern unsigned char FontTable16x16[98][2*16];
extern MMP_USHORT gsOSDLineNum;
#endif

void  pcam_SetOsdString2YuvBuffer(const char *pStr, MMP_SHORT nStrLen, MMP_ULONG ulYBufAddr, MMP_ULONG ulUVBufAddr, MMP_ULONG lineoffset)
{
	MMP_UBYTE   Value;
	MMP_USHORT  h, w;
	MMP_UBYTE   *CurYPtr;
	MMP_USHORT  *CurUVPtr;

    if (gbOSDDisplayEn==0) {
        return;
    }

    CurYPtr = (MMP_UBYTE *)(ulYBufAddr);
    CurUVPtr = (MMP_USHORT *)(ulUVBufAddr);

#if (CUSTOMER == ANW)

    switch(m_usCurrentW){
    case 1280:  // font is 12x14
        for (h = 0; h < 14; h++) {
	        for (w = 0; w < (nStrLen * 12); w++) {
	        
                Value = FontTable12x14[pStr[w/12]-' '][(h<<1)+((w%12)>>3)];

                if (Value & (1 << (7 - ((w % 12) & 7)))) {
                    *(CurYPtr + w) =  gbOSDForeColorY;						
            #if 1
                    if(((w&1)==0) && ((h&1)==0)) {
                        *(CurUVPtr + (w>>1)) = (gsOSDForeColorUV);
                    }
            #endif
                }
    #if (MOVE_OSD_USING_DMA)
                else{
                    *(CurYPtr + w) =  gbOSDBackColorY;  // fill background color						
            #if 1
                    if(((w&1)==0) && ((h&1)==0)) {
                        *(CurUVPtr + (w>>1)) = (gsOSDBackColorUV);
                    }
            #endif
                }
    #endif
            }
            CurYPtr += lineoffset; //(gsOSD_WIDTH_MAX_WORD*16);
            if((h&1)==0){
                CurUVPtr += (lineoffset >> 1); //((gsOSD_WIDTH_MAX_WORD*16) >> 1);
            }
        }
        break;

    case 640:  // font is 8x10
        for (h = 0; h < 10; h++) {
	        for (w = 0; w < (nStrLen << 3); w++) {

                Value = FontTable8x10[pStr[w>>3]-' '][h+((w&7)>>3)];

                if (Value & (1 << (7 - (w & 7)))) {
                    *(CurYPtr + w) =  gbOSDForeColorY;						
            #if 1
                    if(((w&1)==0) && ((h&1)==0)) {
                        *(CurUVPtr + (w>>1)) = (gsOSDForeColorUV);
                    }
            #endif
                }
    #if (MOVE_OSD_USING_DMA)
                else{
                    *(CurYPtr + w) =  gbOSDBackColorY;  // fill background color						
            #if 1
                    if(((w&1)==0) && ((h&1)==0)) {
                        *(CurUVPtr + (w>>1)) = (gsOSDBackColorUV);
                    }
            #endif
                }
    #endif
            }
            CurYPtr += lineoffset; //(gsOSD_WIDTH_MAX_WORD*16);
            if((h&1)==0){
                CurUVPtr += (lineoffset >> 1); //((gsOSD_WIDTH_MAX_WORD*16) >> 1);
            }
        }
        break;

    case 320:  // font is 6x8
    default:
        for (h = 0; h < 8; h++) {
	        for (w = 0; w < (nStrLen * 6); w++) {
	        
                Value = FontTable6x8[pStr[w/6]-' '][h+((w%6)>>3)];

                if (Value & (1 << (7 - ((w % 6) & 7)))) {
                    *(CurYPtr + w) =  gbOSDForeColorY;						
            #if 1
                    if(((w&1)==0) && ((h&1)==0)) {
                        *(CurUVPtr + (w>>1)) = (gsOSDForeColorUV);
                    }
            #endif
                }
    #if (MOVE_OSD_USING_DMA)
                else{
                    *(CurYPtr + w) =  gbOSDBackColorY;  // fill background color						
            #if 1
                    if(((w&1)==0) && ((h&1)==0)) {
                        *(CurUVPtr + (w>>1)) = (gsOSDBackColorUV);
                    }
            #endif
                }
    #endif
            }
            CurYPtr += lineoffset; //(gsOSD_WIDTH_MAX_WORD*16);
            if((h&1)==0){
                CurUVPtr += (lineoffset >> 1); //((gsOSD_WIDTH_MAX_WORD*16) >> 1);
            }
        }
        break;
    }

    
#else  // other customer

    if(m_usCurrentW != 160){  // font is 16x16
        for (h = 0; h < 16; h++) {
	        for (w = 0; w < (nStrLen << 4); w++) {
#if RES_OSD_H24_FONT
                if(((w>>4) == 19)&&(pStr[19] - ' ')) {
                    Value = FontTable16x16[96+AM0_PM1_state][(h<<1)+((w&15)>>3)];
                }
                else {
                    Value = FontTable16x16[pStr[w>>4]-' '][(h<<1)+((w&15)>>3)];
                }
#else
                Value = 0;
#endif
                if (Value & (1 << (7 - (w & 7)))) {
                    *(CurYPtr + w) =  gbOSDForeColorY;						
            #if 1
                    if(((w&1)==0) && ((h&1)==0)) {
                        *(CurUVPtr + (w>>1)) = (gsOSDForeColorUV);
                    }
            #endif
                }
    #if (MOVE_OSD_USING_DMA)
                else{
                    *(CurYPtr + w) =  gbOSDBackColorY;  // fill background color						
            #if 1
                    if(((w&1)==0) && ((h&1)==0)) {
                        *(CurUVPtr + (w>>1)) = (gsOSDBackColorUV);
                    }
            #endif
                }
    #endif
            }
            CurYPtr += lineoffset; //(gsOSD_WIDTH_MAX_WORD*16);
            if((h&1)==0){
                CurUVPtr += (lineoffset >> 1); //((gsOSD_WIDTH_MAX_WORD*16) >> 1);
            }
        }
    }    
    else {  // font is 8x10
        for (h = 0; h < 10; h++) {
	        for (w = 0; w < (nStrLen << 3); w++) {

                Value = FontTable8x10[pStr[w>>3]-' '][h+((w&7)>>3)];

                if (Value & (1 << (7 - (w & 7)))) {
                    *(CurYPtr + w) =  gbOSDForeColorY;						
            #if 1
                    if(((w&1)==0) && ((h&1)==0)) {
                        *(CurUVPtr + (w>>1)) = (gsOSDForeColorUV);
                    }
            #endif
                }
    #if (MOVE_OSD_USING_DMA)
                else{
                    *(CurYPtr + w) =  gbOSDBackColorY;  // fill background color						
            #if 1
                    if(((w&1)==0) && ((h&1)==0)) {
                        *(CurUVPtr + (w>>1)) = (gsOSDBackColorUV);
                    }
            #endif
                }
    #endif
            }
            CurYPtr += lineoffset; //(gsOSD_WIDTH_MAX_WORD*16);
            if((h&1)==0){
                CurUVPtr += (lineoffset >> 1); //((gsOSD_WIDTH_MAX_WORD*16) >> 1);
            }
        }
    }
    
#endif    
}

/**
@brief	transfer pdTextString(string) to strTextString(string) with length
*/
MMP_USHORT pcam_Osd_SetTextString(MMP_BYTE* pdTextString, MMP_SHORT pdTextStringLength)
{

    if(pdTextStringLength > gsOSD_WIDTH_MAX_WORD) { pdTextStringLength = gsOSD_WIDTH_MAX_WORD; }
	MEMSET(strCustomerData, 0, sizeof(strCustomerData));
    MEMCPY(strCustomerData, pdTextString, pdTextStringLength);

    return PCAM_ERROR_NONE;
}

void _____RTC_Function_________(void){return;} //dummy

//------------------------------------------------------------------------------
//  Function    : PCAM_Initialize
//  Description :
//------------------------------------------------------------------------------
/**
@brief The intialize code that should be called once after system power-up

 The intialize code that should be called once after system power-up
 Parameters:
 @retval PCAM_ERROR_NONE or PCAM_SYS_ERR. // PCAM_ERROR_NONE: Success, PCAM_SYS_ERR: Fail
*/
MMP_USHORT pcam_RTC_UpdateTime(void)
{
#if ENABLE_RTC_ON_AIT
    MMP_ERR status;

    if(gbSetDateTimeComplete){
#if (EXT_RTC_CHIP==RTC_BQ32000)
        //status = BQ32000_rtc_get_time(&m_sRTCTime.uwYear, &m_sRTCTime.uwMonth, &m_sRTCTime.uwDay, &m_sRTCTime.uwHour, &m_sRTCTime.uwMinute, &m_sRTCTime.uwSecond);
#endif
    	if(status != MMP_ERR_NONE) {
    		return PCAM_SYS_ERR;
    	}
    }

#else
    MMP_ULONG MonthUP = 0;
    m_sRTCTime.uwSecond++;
    if(m_sRTCTime.uwSecond > 59){
        m_sRTCTime.uwSecond = 0;
        m_sRTCTime.uwMinute++;
        if(m_sRTCTime.uwMinute > 59){
            m_sRTCTime.uwMinute = 0;
            m_sRTCTime.uwHour++;
            if((m_sRTCTime.uwHour > 23) || (AM0_PM1_state && (m_sRTCTime.uwHour > 11))){
                if(m_sRTCTime.uwHour > 23)
                    m_sRTCTime.uwHour = 0;
                m_sRTCTime.uwDay++;
                if((m_sRTCTime.uwMonth==4)||(m_sRTCTime.uwMonth==6)||
                   (m_sRTCTime.uwMonth==9)||(m_sRTCTime.uwMonth==11)){
                    if(m_sRTCTime.uwDay > 30)
                        MonthUP = 1;
                }
                else if(m_sRTCTime.uwMonth==2){
                    if(((m_sRTCTime.uwYear%4)==0)&&(((m_sRTCTime.uwYear%100)!=0)||((m_sRTCTime.uwYear%400)==0))){
                        if(m_sRTCTime.uwDay > 29)
                            MonthUP = 1;
                    }
                    else{
                        if(m_sRTCTime.uwDay > 28)
                            MonthUP = 1;
                    }
                }
                else{
                    if(m_sRTCTime.uwDay > 31)
                        MonthUP = 1;
                }
                if(MonthUP){
                    m_sRTCTime.uwDay = 1;
                    m_sRTCTime.uwMonth++;
                    if(m_sRTCTime.uwMonth > 12){
                        m_sRTCTime.uwMonth = 1;
                        m_sRTCTime.uwYear++;
                    }
                }
            }
        }
    }
#endif

    pcam_RTC_UpdateTimeString();

	return PCAM_ERROR_NONE;
}

MMP_USHORT pcam_RTC_UpdateTimeString(void)
{
	MEMSET(strTimeDate, 0, 64);
    pcam_TransferDateTime2String(&m_sRTCTime, strTimeDate, PCAM_ACC_TIMESTAMP_DATE_AND_TIME, ubOSDDateFmt);

	return PCAM_ERROR_NONE;
}

PCAM_RTC_TIME pcam_RTC_GetTime(void)
{
	return m_sRTCTime;
}

//------------------------------------------------------------------------------
//  Function    : pcam_SetClock
//  Description :
//------------------------------------------------------------------------------
/**
 @brief Set RTC

 Set RTC value.
 Parameters:
 @param[in] pwYear Set year
 @param[in] pwMonth Set Month
 @param[in] pwDay Set Day
 @param[in] pwHour Set Hour
 @param[in] pwMinute Set Minute
 @param[in] pwSecond Set Second
 @retval PCAM_ERROR_NONE or PCAM_SYS_ERR. // PCAM_ERROR_NONE: Success, PCAM_SYS_ERR: Fail
*/

MMP_USHORT pcam_SetClock(MMP_USHORT pwYear, MMP_USHORT pwMonth, MMP_USHORT pwDay, MMP_USHORT pwHour, MMP_USHORT pwMinute, MMP_USHORT pwSecond)
{
#if ENABLE_RTC_ON_AIT
	MMP_ERR status;

    //status = BQ32000_rtc_set_time(pwYear, pwMonth, pwDay, pwHour, pwMinute, pwSecond);
	
    if(status != MMP_ERR_NONE) {
        return PCAM_SYS_ERR;
    }

#else
    dbg_printf(1,"pcam_SetClock:%d/%d/%d %d:%d:%d. \r\n", pwYear, pwMonth, pwDay, pwHour, pwMinute, pwSecond);
    nPcamOsdTimerCount = 0;
    m_sRTCTime.uwSecond = pwSecond;
    m_sRTCTime.uwMinute = pwMinute;
    if(ubOSDTimeFmt == 1){  // 12 Hour
        if(pwHour > 12){
            AM0_PM1_state = 1;
            bTimeFmtflag = 1;
            m_sRTCTime.uwHour = pwHour - 12;
        }
        else if(pwHour == 12){
            AM0_PM1_state = 1;
            bTimeFmtflag = 0;
            m_sRTCTime.uwHour = pwHour;
        }
        else if(pwHour > 0){
            AM0_PM1_state = 0;
            bTimeFmtflag = 1;
            m_sRTCTime.uwHour = pwHour;
        }
        else if(pwHour == 0){
            AM0_PM1_state = 0;
            bTimeFmtflag = 0;
            m_sRTCTime.uwHour = pwHour + 12;
        }
    }
    else{  // 24 Hour
        m_sRTCTime.uwHour = pwHour;
    }
    m_sRTCTime.uwDay = pwDay;
    m_sRTCTime.uwMonth = pwMonth;
    m_sRTCTime.uwYear = pwYear;
#endif

    gbSetDateTimeComplete = 1;

	return PCAM_ERROR_NONE;
}

#endif//SUPPORT_OSD_FUNC

